﻿/*----------------------------------------------------------------
// Copyright (C) 2014 苏州麦迪斯顿医疗科技股份有限公司
// 文件名：ServiceProxy.cs
// 文件功能描述：ServiceProxy
// 创建标识：顾伟伟-2014-04-03
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CPOE.Framework.Throttling;
using System.ServiceModel.Channels;

namespace CPOE.Framework.Throttling
{
    /// <summary>
    /// 一个使用DynamicChannelFactory随机创建信道的客户端服务代理基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceProxy<T> : IDisposable where T : class
    {
        private ChannelFactory<T> _channelFactory;
        /// <summary>
        /// 服务契约
        /// </summary>
        protected T _proxy;
        /// <summary>
        /// 根据endpointName创建一个服务代理
        /// </summary>
        /// <param name="endpointName"></param>
        public ServiceProxy(string endpointName)
        {
            _channelFactory = new DynamicChannelFactory<T>(endpointName);

            _proxy = _channelFactory.CreateChannel();
        }
        /// <summary>
        /// 关闭通道
        /// </summary>
        public void Dispose()
        {
            if (_channelFactory != null)
            {
                try
                {
                    _channelFactory.Close();
                }
                catch
                {
                    _channelFactory.Abort();
                }
            }


        }
    }

    
}
